package mongo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.bson.types.ObjectId;

import tool.DateUtils;
import vo.Item;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;

public class ItemDAO {
	private static Mongo m;
	private static DB db;
	private static DBCollection coll;

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		Item item = ItemDAO.getById("4fd4af1cf7b1a9a550370a48");
		System.out.println(item.getContent());
	}

	public static DBCollection getDBCollection(String collectionName) throws Exception {
		if (m == null)
			m = new Mongo("182.50.3.54", 27017);
		if (db == null)
			db = m.getDB("yuqing");
		if (coll == null)
			coll = db.getCollection(collectionName);
		return coll;
	}

	public static List<Item> getAllItems(int start, int pageSize) throws Exception {
		List<Item> items = new ArrayList<Item>();
		DBCollection coll = ItemDAO.getDBCollection("item");
		BasicDBObject obj = new BasicDBObject();
		DBCursor cursor = coll.find(obj).skip(start).limit(pageSize);
		while (cursor.hasNext()) {
			DBObject it = cursor.next();
			ObjectId id = (ObjectId) it.get("_id");
			String title = (String) it.get("title");
			String content = (String) it.get("content");
			Date pubTime = (Date) it.get("pubTime");
			String url = (String) it.get("url");
			String type = (String) it.get("type");
			String author = (String) it.get("author");
			String authorIP = (String) it.get("authorIP");
			String source = (String) it.get("source");
			String replyNum = (String) it.get("replyNum");

			Item item = new Item();
			item.setId(id.toString());
			item.setTitle(title);
			item.setContent(content);
			item.setPubTime(pubTime);
			item.setUrl(url);
			item.setType(type);
			item.setAuthor(author);
			item.setIp(authorIP);
			item.setSource(source);
			item.setReplyNum(Integer.parseInt(replyNum));
			items.add(item);
		}
		cursor.close();
		return items;
	}

	public static List<Item> getAllItemsByKeyword(int start, int pageSize, String keyword) throws Exception {
		List<Item> items = new ArrayList<Item>();
		DBCollection coll = ItemDAO.getDBCollection("item");
		BasicDBObject obj = new BasicDBObject();
		obj.put("title", keyword);
		DBCursor cursor = coll.find(obj).skip(start).limit(pageSize);
		while (cursor.hasNext()) {
			DBObject it = cursor.next();
			ObjectId id = (ObjectId) it.get("_id");
			String title = (String) it.get("title");
			String content = (String) it.get("content");
			Date pubTime = (Date) it.get("pubTime");
			String url = (String) it.get("url");
			String type = (String) it.get("type");
			String author = (String) it.get("author");
			String authorIP = (String) it.get("authorIP");
			String source = (String) it.get("source");

			Item item = new Item();
			item.setId(id.toString());
			item.setTitle(title);
			item.setContent(content);
			item.setPubTime(pubTime);
			item.setUrl(url);
			item.setType(type);
			item.setAuthor(author);
			item.setIp(authorIP);
			item.setSource(source);
			items.add(item);
		}
		cursor.close();
		return items;
	}

	public static List<Item> getAllItemsBySourceId(int start, int pageSize, String sourceId) throws Exception {
		List<Item> items = new ArrayList<Item>();
		DBCollection coll = ItemDAO.getDBCollection("item");
		BasicDBObject obj = new BasicDBObject();
		obj.put("source", sourceId);
		DBCursor cursor = coll.find(obj).skip(start).limit(pageSize);
		while (cursor.hasNext()) {
			DBObject it = cursor.next();
			ObjectId id = (ObjectId) it.get("_id");
			String title = (String) it.get("title");
			String content = (String) it.get("content");
			Date pubTime = (Date) it.get("pubTime");
			String url = (String) it.get("url");
			String type = (String) it.get("type");
			String author = (String) it.get("author");
			String authorIP = (String) it.get("authorIP");
			String source = (String) it.get("source");

			Item item = new Item();
			item.setId(id.toString());
			item.setTitle(title);
			item.setContent(content);
			item.setPubTime(pubTime);
			item.setUrl(url);
			item.setType(type);
			item.setAuthor(author);
			item.setIp(authorIP);
			item.setSource(source);
			items.add(item);
		}
		cursor.close();
		return items;
	}

	public static List<Item> getAllItemsByCondition(int start, int pageSize, String startDate, String endDate, String sourceId) throws Exception {
		List<Item> items = new ArrayList<Item>();
		DBCollection coll = ItemDAO.getDBCollection("item");
		BasicDBObject obj = new BasicDBObject();
		if (startDate != null && endDate != null)
			obj.put("pubTime", new BasicDBObject("$gte", DateUtils.str2Date(startDate + " 00:00:00")).append("$lte", DateUtils.str2Date(endDate + " 00:00:00")));
		if (sourceId != null)
			obj.put("source", sourceId);
		DBCursor cursor = coll.find(obj).skip(start).limit(pageSize);
		while (cursor.hasNext()) {
			DBObject it = cursor.next();
			ObjectId id = (ObjectId) it.get("_id");
			String title = (String) it.get("title");
			String content = (String) it.get("content");
			Date pubTime = (Date) it.get("pubTime");
			String url = (String) it.get("url");// TODO:url待开放
			String type = (String) it.get("type");
			String author = (String) it.get("author");
			String authorIP = (String) it.get("authorIP");
			String source = (String) it.get("source");

			Item item = new Item();
			item.setId(id.toString());
			item.setTitle(title);
			item.setContent(content);
			item.setPubTime(pubTime);
			item.setUrl(url);// TODO:url待开放
			item.setType(type);
			item.setAuthor(author);
			item.setIp(authorIP);
			item.setSource(source);
			items.add(item);
		}
		cursor.close();
		return items;
	}

	public static List<Item> getAllItems() throws Exception {
		List<Item> items = new ArrayList<Item>();
		DBCollection coll = ItemDAO.getDBCollection("item");
		BasicDBObject obj = new BasicDBObject();
		DBCursor cursor = coll.find(obj);
		while (cursor.hasNext()) {
			DBObject it = cursor.next();
			ObjectId id = (ObjectId) it.get("_id");
			String title = (String) it.get("title");
			String content = (String) it.get("content");
			Date pubTime = (Date) it.get("pubTime");
			String url = (String) it.get("url");
			String type = (String) it.get("type");
			String author = (String) it.get("author");
			String authorIP = (String) it.get("authorIP");
			String source = (String) it.get("source");

			Item item = new Item();
			item.setId(id.toString());
			item.setTitle(title);
			item.setContent(content);
			item.setPubTime(pubTime);
			item.setUrl(url);
			item.setType(type);
			item.setAuthor(author);
			item.setIp(authorIP);
			item.setSource(source);
			items.add(item);
		}
		cursor.close();
		return items;
	}

	public static Item getById(String itemId) throws Exception {
		DBCollection coll = ItemDAO.getDBCollection("item");
		BasicDBObject obj = new BasicDBObject();
		System.out.println("input item:" + itemId);
		ObjectId objId = new ObjectId(itemId);
		obj.put("_id", objId);

		DBCursor cursor = coll.find(obj);
		if (cursor.hasNext()) {
			System.out.println("has something");
			DBObject it = cursor.next();
			String title = (String) it.get("title");
			String content = (String) it.get("content");
			Date pubTime = (Date) it.get("pubTime");
			String url = (String) it.get("url");
			String type = (String) it.get("type");
			String author = (String) it.get("author");
			String authorIP = (String) it.get("authorIP");
			String source = (String) it.get("source");
			ObjectId id = (ObjectId) it.get("_id");

			// String id = itemId;
			Item item = new Item();
			item.setId(id.toString());
			item.setTitle(title);
			item.setContent(content);
			item.setPubTime(pubTime);
			item.setUrl(url);
			item.setType(type);
			item.setAuthor(author);
			item.setSource(source);
			item.setIp(authorIP);
			return item;
		}
		cursor.close();
		return null;
	}

	public static int getTotalCount() throws Exception {
		DBCollection coll = ItemDAO.getDBCollection("item");
		BasicDBObject obj = new BasicDBObject();
		DBCursor cursor = coll.find(obj);
		int result = cursor.size();
		cursor.close();
		return result;
	}

	public static int getTotalCountByKeyword(String keyword) throws Exception {
		DBCollection coll = ItemDAO.getDBCollection("item");
		BasicDBObject obj = new BasicDBObject();
		DBCursor cursor = coll.find(obj);
		obj.put("title", keyword);
		int result = cursor.size();
		cursor.close();
		return result;
	}

	public static int getTotalCountBySourceId(String sourceId) throws Exception {
		DBCollection coll = ItemDAO.getDBCollection("item");
		BasicDBObject obj = new BasicDBObject();
		DBCursor cursor = coll.find(obj);
		obj.put("source", sourceId);
		int result = cursor.size();
		cursor.close();
		return result;
	}

	public static int getTotalCountByCondition(String startDate, String endDate, String sourceId) throws Exception {
		DBCollection coll = ItemDAO.getDBCollection("item");
		BasicDBObject obj = new BasicDBObject();
		DBCursor cursor = coll.find(obj);
		if (startDate != null && startDate.length() != 0)
			if (endDate != null && endDate.length() != 0)
				obj.put("pubTime", new BasicDBObject("$gte", DateUtils.str2Date(startDate + " 00:00:00")).append("$lte", DateUtils.str2Date(endDate + " 00:00:00")));
		if (sourceId != null)
			obj.put("source", sourceId);
		int result = cursor.size();
		cursor.close();
		return result;
	}

	public static int getCountByTimeRange(Date start, Date end) throws Exception {
		DBCollection coll = getDBCollection("item");
		BasicDBObject obj = new BasicDBObject();
		obj.put("pubTime", new BasicDBObject("$gte", start).append("$lte", end));
		DBCursor cursor = coll.find(obj);
		int result = cursor.size();
		System.out.println(cursor.size());
		cursor.close();
		return result;
	}

	public static List<Item> getItemsByTimeRange(Date start, Date end) throws Exception {
		List<Item> items = new ArrayList<Item>();
		DBCollection coll = getDBCollection("item");
		BasicDBObject obj = new BasicDBObject();
		obj.put("pubTime", new BasicDBObject("$gte", start).append("$lte", end));
		DBCursor cursor = coll.find(obj);
		while (cursor.hasNext()) {
			DBObject it = cursor.next();
			ObjectId id = (ObjectId) it.get("_id");
			String title = (String) it.get("title");
			String content = (String) it.get("content");
			Date pubTime = (Date) it.get("pubTime");
			String url = (String) it.get("url");
			String type = (String) it.get("type");
			String author = (String) it.get("author");
			String authorIP = (String) it.get("authorIP");

			Item item = new Item();
			item.setId(id.toString());
			item.setTitle(title);
			item.setContent(content);
			item.setPubTime(pubTime);
			item.setUrl(url);
			item.setType(type);
			item.setAuthor(author);
			item.setIp(authorIP);
			items.add(item);
		}
		System.out.println(cursor.size());
		cursor.close();
		return items;
	}

	public static Item[] list2Array(List<Item> items) {
		Item[] itemArr = new Item[items.size()];
		for (int i = 0; i < items.size(); i++) {
			itemArr[i] = items.get(i);
		}
		return itemArr;
	}

	public static ArrayList<Item> array2List(Item[] itemArr) {
		ArrayList<Item> items = new ArrayList<Item>();
		for (int i = 0; i < itemArr.length; i++) {
			items.add(itemArr[i]);
		}
		return items;
	}

	public static List<Item> sortByReplyNum(List<Item> items) {
		Item[] itemArr = list2Array(items);
		for (int i = 0; i < itemArr.length; i++) {
			for (int j = 0; j < i + 1; j++) {
				if (itemArr[j].getReplyNum() < itemArr[i].getReplyNum()) {
					Item x = itemArr[j];
					itemArr[j] = itemArr[i];
					itemArr[i] = x;
				}
			}
		}
		return array2List(itemArr);
	}

}